﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Demo01.Repsotories;
using System.Windows.Forms;
using WindowsFormsApp1.Models;

namespace WindowsFormsApp1.Respostories
{
    public class THRecordsRespostry
    {
        //获取仓库数据库
        public static List<THRecord> GetTHRecords(int SlaverId)
        {
            List<THRecord> THRecords = new List<THRecord>();
            SqlDataReader reader = null;
            string sql = "SELECT * FROM THRecords WHERE SlaveId=@SlaverId";
            try
            {
                reader = DBHelper.Query(sql, new SqlParameter("@SlaverId", SlaverId));
                while (reader.Read())
                {
                    THRecord THRecord = new THRecord();
                    THRecord.Id = (int)reader["Id"];
                    THRecord.SlaveId = (int)reader["SlaveId"];
                    THRecord.Temperature = reader.GetDouble(2);
                    THRecord.Humidity = reader.GetDouble(3);
                    THRecord.Creation = reader.GetDateTime(4);
                    THRecords.Add(THRecord);
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("读取全部温湿度数据库失败 ：" + ex.Message);
            }
            finally
            {
                reader.Close();
            }
            return THRecords;
        }


        /// <summary>
        /// 添加仓库记录
        /// </summary>
        /// <param name="THRecord"></param>
        /// <returns></returns>
        public static bool AddTHRecord(THRecord THRecord)
        {
            THRecord.Id = GetLastTHRecord().Id + 1;
            //string sql = "INSERT INTO THRecords( Id,SlaveId,Temperature,Humidity,Creation) VALUES( @Id,@SlaveId,@Temperature,@Humidity,@Creation)";
            string sql = "INSERT INTO THRecords( SlaveId,Temperature,Humidity,Creation) VALUES( @SlaveId,@Temperature,@Humidity,@Creation)";
            SqlParameter[] parameters = {
                //new SqlParameter("@Id",THRecord.Id),
                new SqlParameter("@SlaveId",THRecord.SlaveId),
                new SqlParameter("@Temperature",THRecord.Temperature),
                new SqlParameter("@Humidity",THRecord.Humidity),
                new SqlParameter("@Creation",THRecord.Creation)
            };
            try
            {
                int count = DBHelper.ExecuteNonQuery(sql, parameters);
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(" 添加仓库记录失败：" + ex.Message);
                return false;
            }

        }

        /// <summary>
        /// 获取最新一条仓库记录
        /// </summary>
        /// <returns></returns>
        public static THRecord GetLastTHRecord()
        {
            THRecord THRecord = new THRecord();
            SqlDataReader reader = null;
            string sql = "SELECT TOP 1 * FROM THRecords ORDER BY Id DESC;";
            try
            {
                reader = DBHelper.Query(sql);
                
                    THRecord.Id = (int)reader["Id"];
                    THRecord.SlaveId = (int)reader["SlaveId"];
                    THRecord.Temperature = reader.GetDouble(2);
                    THRecord.Humidity = reader.GetDouble(3);
                    THRecord.Creation = reader.GetDateTime(4);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(" 读取最新温湿度数据记录：" + ex.Message);
            }
            finally
            {
                reader.Close();
            }

            return THRecord;
        }
    }
}

